<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="医代ID" prop="agentMrId">
              <el-input v-model="queryParams.agentMrId" placeholder="请输入医代ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="系统用户ID" prop="sysUserId">
              <el-input v-model="queryParams.sysUserId" placeholder="请输入系统用户ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="充值订单ID" prop="rechargeId">
              <el-input v-model="queryParams.rechargeId" placeholder="请输入充值订单ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="充值编号" prop="rechargeNo">
              <el-input v-model="queryParams.rechargeNo" placeholder="请输入充值编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="支付单号" prop="payNo">
              <el-input v-model="queryParams.payNo" placeholder="请输入支付单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="支付类型" prop="payWay">
              <el-select v-model="queryParams.payWay" placeholder="请选择支付类型" clearable>
                <el-option v-for="dict in busi_store_recharge_pay_way" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="充值金额" prop="rechargeAmount">
              <el-input v-model="queryParams.rechargeAmount" placeholder="请输入充值金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="充值次数" prop="rechargeBalance">
              <el-input v-model="queryParams.rechargeBalance" placeholder="请输入充值次数" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="退款单号" prop="refundNo">
              <el-input v-model="queryParams.refundNo" placeholder="请输入退款单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="退款类型" prop="refundWay">
              <el-select v-model="queryParams.refundWay" placeholder="请选择退款类型" clearable>
                <el-option v-for="dict in busi_store_refund_details_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                <el-option v-for="dict in busi_store_refund_details_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table v-loading="loading" :data="agentMrRefundDetailsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="true" label="ID" align="center" prop="id" />
        <el-table-column label="退款申请ID" align="center" prop="refundApplyId" />
        <el-table-column label="医代ID" align="center" prop="agentMrId" />
        <el-table-column label="系统用户ID" align="center" prop="sysUserId" />
        <el-table-column label="充值订单ID" align="center" prop="rechargeId" />
        <el-table-column label="充值编号" align="center" prop="rechargeNo" />
        <el-table-column label="支付单号" align="center" prop="payNo" />
        <el-table-column label="支付类型" align="center" prop="payWay">
          <template #default="scope">
            <dict-tag :options="busi_store_recharge_pay_way" :value="scope.row.payWay" />
          </template>
        </el-table-column>
        <el-table-column label="充值金额" align="center" prop="rechargeAmount" />
        <el-table-column label="充值次数" align="center" prop="rechargeBalance" />
        <el-table-column label="退款单号" align="center" prop="refundNo" />
        <el-table-column label="退款类型" align="center" prop="refundWay">
          <template #default="scope">
            <dict-tag :options="busi_store_refund_details_type" :value="scope.row.refundWay" />
          </template>
        </el-table-column>
        <el-table-column label="退款结果" align="center" prop="refundResult" />
        <el-table-column label="申请退款次数" align="center" prop="applyRefundBalance" />
        <el-table-column label="申请退款金额" align="center" prop="applyRefundAmount" />
        <el-table-column label="实际退款次数" align="center" prop="actualRefundBalance" />
        <el-table-column label="实际退款金额" align="center" prop="actualRefundAmount" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="busi_store_refund_details_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                v-hasPermi="['business:agentMrRefundDetails:edit']"
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                v-hasPermi="['business:agentMrRefundDetails:remove']"
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column> -->
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改医代退款详情对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="agentMrRefundDetailsFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退款申请ID" prop="refundApplyId">
          <el-input v-model="form.refundApplyId" placeholder="请输入退款申请ID" />
        </el-form-item>
        <el-form-item label="医代ID" prop="agentMrId">
          <el-input v-model="form.agentMrId" placeholder="请输入医代ID" />
        </el-form-item>
        <el-form-item label="系统用户ID" prop="sysUserId">
          <el-input v-model="form.sysUserId" placeholder="请输入系统用户ID" />
        </el-form-item>
        <el-form-item label="充值订单ID" prop="rechargeId">
          <el-input v-model="form.rechargeId" placeholder="请输入充值订单ID" />
        </el-form-item>
        <el-form-item label="充值编号" prop="rechargeNo">
          <el-input v-model="form.rechargeNo" placeholder="请输入充值编号" />
        </el-form-item>
        <el-form-item label="支付单号" prop="payNo">
          <el-input v-model="form.payNo" placeholder="请输入支付单号" />
        </el-form-item>
        <el-form-item label="支付类型" prop="payWay">
          <el-radio-group v-model="form.payWay">
            <el-radio v-for="dict in busi_store_recharge_pay_way" :key="dict.value" :value="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="充值金额" prop="rechargeAmount">
          <el-input v-model="form.rechargeAmount" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="充值次数" prop="rechargeBalance">
          <el-input v-model="form.rechargeBalance" placeholder="请输入充值次数" />
        </el-form-item>
        <el-form-item label="退款单号" prop="refundNo">
          <el-input v-model="form.refundNo" placeholder="请输入退款单号" />
        </el-form-item>
        <el-form-item label="退款类型" prop="refundWay">
          <el-radio-group v-model="form.refundWay">
            <el-radio v-for="dict in busi_store_refund_details_type" :key="dict.value" :value="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="退款结果" prop="refundResult">
          <el-input v-model="form.refundResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请退款次数" prop="applyRefundBalance">
          <el-input v-model="form.applyRefundBalance" placeholder="请输入申请退款次数" />
        </el-form-item>
        <el-form-item label="申请退款金额" prop="applyRefundAmount">
          <el-input v-model="form.applyRefundAmount" placeholder="请输入申请退款金额" />
        </el-form-item>
        <el-form-item label="实际退款次数" prop="actualRefundBalance">
          <el-input v-model="form.actualRefundBalance" placeholder="请输入实际退款次数" />
        </el-form-item>
        <el-form-item label="实际退款金额" prop="actualRefundAmount">
          <el-input v-model="form.actualRefundAmount" placeholder="请输入实际退款金额" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in busi_store_refund_details_status" :key="dict.value" :value="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="JSON串存储扩展字段" prop="otherVal">
          <el-input v-model="form.otherVal" placeholder="请输入JSON串存储扩展字段" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <!-- <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button> -->
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AgentMrRefundDetails" lang="ts">
import { listAgentMrRefundDetails, getAgentMrRefundDetails } from '@/api/business/agentMrRefundDetails';
import { AgentMrRefundDetailsVO, AgentMrRefundDetailsQuery, AgentMrRefundDetailsForm } from '@/api/business/agentMrRefundDetails/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { busi_store_refund_details_status, busi_store_refund_details_type, busi_store_recharge_pay_way } = toRefs<any>(
  proxy?.useDict('busi_store_refund_details_status', 'busi_store_refund_details_type', 'busi_store_recharge_pay_way')
);

const agentMrRefundDetailsList = ref<AgentMrRefundDetailsVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const agentMrRefundDetailsFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: AgentMrRefundDetailsForm = {
  id: undefined,
  refundApplyId: undefined,
  agentMrId: undefined,
  sysUserId: undefined,
  rechargeId: undefined,
  rechargeNo: undefined,
  payNo: undefined,
  payWay: undefined,
  rechargeAmount: undefined,
  rechargeBalance: undefined,
  refundNo: undefined,
  refundWay: undefined,
  refundResult: undefined,
  applyRefundBalance: undefined,
  applyRefundAmount: undefined,
  actualRefundBalance: undefined,
  actualRefundAmount: undefined,
  status: undefined,
  otherVal: undefined,
  remark: undefined
};
const data = reactive<PageData<AgentMrRefundDetailsForm, AgentMrRefundDetailsQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    refundApplyId: undefined,
    agentMrId: undefined,
    sysUserId: undefined,
    rechargeId: undefined,
    rechargeNo: undefined,
    payNo: undefined,
    payWay: undefined,
    rechargeAmount: undefined,
    rechargeBalance: undefined,
    refundNo: undefined,
    refundWay: undefined,
    refundResult: undefined,
    applyRefundBalance: undefined,
    applyRefundAmount: undefined,
    actualRefundBalance: undefined,
    actualRefundAmount: undefined,
    status: undefined,
    otherVal: undefined,
    createTime: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: 'ID不能为空', trigger: 'blur' }],
    refundApplyId: [{ required: true, message: '退款申请ID不能为空', trigger: 'blur' }],
    agentMrId: [{ required: true, message: '医代ID不能为空', trigger: 'blur' }],
    sysUserId: [{ required: true, message: '系统用户ID不能为空', trigger: 'blur' }],
    rechargeId: [{ required: true, message: '充值订单ID不能为空', trigger: 'blur' }],
    rechargeNo: [{ required: true, message: '充值编号不能为空', trigger: 'blur' }],
    payNo: [{ required: true, message: '支付单号不能为空', trigger: 'blur' }],
    payWay: [{ required: true, message: '支付类型不能为空', trigger: 'change' }],
    rechargeAmount: [{ required: true, message: '充值金额不能为空', trigger: 'blur' }],
    rechargeBalance: [{ required: true, message: '充值次数不能为空', trigger: 'blur' }],
    refundNo: [{ required: true, message: '退款单号不能为空', trigger: 'blur' }],
    refundWay: [{ required: true, message: '退款类型不能为空', trigger: 'change' }],
    refundResult: [{ required: true, message: '退款结果不能为空', trigger: 'blur' }],
    applyRefundBalance: [{ required: true, message: '申请退款次数不能为空', trigger: 'blur' }],
    applyRefundAmount: [{ required: true, message: '申请退款金额不能为空', trigger: 'blur' }],
    actualRefundBalance: [{ required: true, message: '实际退款次数不能为空', trigger: 'blur' }],
    actualRefundAmount: [{ required: true, message: '实际退款金额不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
    otherVal: [{ required: true, message: 'JSON串存储扩展字段不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询医代退款详情列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAgentMrRefundDetails(queryParams.value);
  agentMrRefundDetailsList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  agentMrRefundDetailsFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: AgentMrRefundDetailsVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
// const handleAdd = () => {
//   reset();
//   dialog.visible = true;
//   dialog.title = '添加医代退款详情';
// };

/** 修改按钮操作 */
// const handleUpdate = async (row?: AgentMrRefundDetailsVO) => {
//   reset();
//   const _id = row?.id || ids.value[0];
//   const res = await getAgentMrRefundDetails(_id);
//   Object.assign(form.value, res.data);
//   dialog.visible = true;
//   dialog.title = '修改医代退款详情';
// };

/** 提交按钮 */
// const submitForm = () => {
//   agentMrRefundDetailsFormRef.value?.validate(async (valid: boolean) => {
//     if (valid) {
//       buttonLoading.value = true;
//       if (form.value.id) {
//         await updateAgentMrRefundDetails(form.value).finally(() => (buttonLoading.value = false));
//       } else {
//         await addAgentMrRefundDetails(form.value).finally(() => (buttonLoading.value = false));
//       }
//       proxy?.$modal.msgSuccess('操作成功');
//       dialog.visible = false;
//       await getList();
//     }
//   });
// };

/** 删除按钮操作 */
// const handleDelete = async (row?: AgentMrRefundDetailsVO) => {
//   const _ids = row?.id || ids.value;
//   await proxy?.$modal.confirm('是否确认删除医代退款详情编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
//   await delAgentMrRefundDetails(_ids);
//   proxy?.$modal.msgSuccess('删除成功');
//   await getList();
// };

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'business/agentMrRefundDetails/export',
    {
      ...queryParams.value
    },
    `agentMrRefundDetails_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
